#include "Box.h"

// 宝箱构造
Box::Box(const Point &p)
{
    SetLocation(p);
    SetSymbol("📦");
}

// 打开宝箱
void Box::OpenBox(Player &player)
{
    if(BoxFlag == 0)
    {
        std::cout << "是否要拆箱？(y/n)";
        char c = 0;
        std::cin >> c;
        if(c == 'y' || c == 'Y')
        {
            system("clear");
            RandomContent();
            std::cout << "见证奇迹的时刻！" << std::endl;
            sleep(1);
            if (gold > 0)
            {
                std::cout << "恭喜你你获得了金币：" << gold << "个" << std::endl;
                sleep(1);
                // 添加金币到玩家
                player.SetGold(player.GetGold() + gold);
                BoxFlag = 1;
            }
            if (diamond > 0)
            {
                std::cout << "恭喜你你获得了钻石：" << diamond << "个" << std::endl;
                sleep(1);
                // 添加钻石到玩家
                player.SetDiamond(player.GetDiamond() + diamond);
                BoxFlag = 1;
            }
            if (monster)
            {
                std::cout << "糟糕！你释放了一个怪兽：" << monster->GetName() << std::endl;
                sleep(1);
                // 进入战斗
                Battle(player, *monster);
                BoxFlag = 1;
            }
        }
    }
      
}

// 随机生成内容
void Box::RandomContent()
{
    // 设定随机种子
    srand(time(nullptr));
    int randValue = rand() % 100; // 生成0~99之间的随机整数
    // 50%概率出金币
    if(randValue < 50)
    {
        gold = rand() % 100 + 1; // 1~100之间的随机金币数量
        diamond = 0;
        monster = nullptr;
    }
    // 30%概率出钻石
    else if(randValue >= 50 && randValue < 80)
    {
        gold = 0;
        diamond = rand() % 50 + 1; // 1~50之间的随机钻石数量
        monster = nullptr;
    }
    // 20%概率出怪兽
    else
    {
        gold = 0;
        diamond = 0;
        int MonsterType = rand() % 3; // 随机生成的怪兽类型
        if (FloorFlag == 1)
        {
            switch (MonsterType)
            {
            case 0:
                monster = std::make_shared<Bone>(GetLocation());
                break;
            case 1:
                monster = std::make_shared<Dinosaur>(GetLocation());
                break;
            case 2:
                monster = std::make_shared<Orge>(GetLocation());
                break;
            }
        }
        else if (FloorFlag == 2)
        {
            switch (MonsterType)
            {
            case 0:
                monster = std::make_shared<Monkey>(GetLocation());
                break;
            case 1:
                monster = std::make_shared<Lizard>(GetLocation());
                break;
            case 2:
                monster = std::make_shared<Wolf>(GetLocation());
                break;
            }
        }
        else if (FloorFlag == 3)
        {
            switch (MonsterType)
            {
            case 0:
                monster = std::make_shared<Fish>(GetLocation());
                break;
            case 1:
                monster = std::make_shared<Zombie>(GetLocation());
                break;
            case 2:
                monster = std::make_shared<ET>(GetLocation());
                break;
            }
        }     
    }
}

